package com.example.lin.homework_yjj_login.activity;

import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.example.lin.homework_yjj_login.R;
import com.example.lin.homework_yjj_login.adapter.WeChatAdpater;
import com.example.lin.homework_yjj_login.model.QingChiFan;
import com.example.lin.homework_yjj_login.utils.HttpUtils;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;

public class HomeActivity extends AppCompatActivity {

    private static final String TAG = "HomeActivity";

    private ListView lv_weChat;

    private WeChatAdpater weChatAdpater = null;
    private SmartRefreshLayout refreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
        loadData();
    }

    private void initView() {
        lv_weChat = findViewById(R.id.lv_weChat);
        weChatAdpater = new WeChatAdpater(HomeActivity.this);
        lv_weChat.setAdapter(weChatAdpater);//绑定适配器

        refreshLayout = findViewById(R.id.refreshLayout);
        //监听加载更多的事件
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                currentPage++;
                loadData();
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //开启上拉加载更多
                refreshLayout.setEnableLoadMore(true);
                currentPage = 0;
                loadData();
            }
        });
    }

    //第几页
    private int currentPage = 0;
    //每页多少条
    private int PageSize = 10;

    /**
     * 加载数据
     */
    private void loadData() {
        String address = "http://api.qingchifan.com/api/event/city.json?";
        HashMap<String, String> map = new HashMap<>();
        map.put("access_token", "6916a7ab1f71e43ac4eadf40a51b4ab1E4DA4EE79DE4C64C449D09318FE0784F");
        map.put("basicCheck", "0");
        map.put("city", "0755");
        map.put("multi", "0");
        map.put("gender", "0");
        map.put("time", "0");
        map.put("age", "0");
        map.put("constellation", "0");
        map.put("occupation", "0");
        map.put("start", currentPage + "");
        map.put("size", PageSize + "");
        map.put("apiVersion", "2.8.0");
        HttpUtils.get(address, map, new HttpUtils.OnHttpCallback() {
            @Override
            public void onSuccess(String response) {
                h.sendMessage(h.obtainMessage(TYPE_SUCCESS, response));
            }

            @Override
            public void onFailure(String error) {
                h.sendMessage(h.obtainMessage(TYPE_FAILURE, error));
            }
        });
    }

    /**
     * 请求成功,子线程给Handler发送的what的值
     */
    public static final int TYPE_SUCCESS = 0xFFAACC;
    /**
     * 请求失败,子线程给Handler发送的what的值
     */
    public static final int TYPE_FAILURE = 0x123456;


    private Handler h = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case TYPE_SUCCESS:
                    //告诉刷新框架,已经刷新成功
                    refreshLayout.finishLoadMore(true);
                    refreshLayout.finishRefresh(true);
                    String s = msg.obj.toString();
                    QingChiFan qingChiFan = new Gson().fromJson(s, QingChiFan.class);
                    Log.d(TAG, "handleMessage: " + qingChiFan.getData().getTotal());
                    if (currentPage == 0) {
                        //修改适配器数据
                        weChatAdpater.setQingChiFan(qingChiFan);
                    } else {
                        //加载更多,合并数据
                        weChatAdpater.addQingChiFan(qingChiFan);
                    }
                    //当加载的总条数 为服务器的总条数,则证明没有更多数据了.
                    if (weChatAdpater.getCount() >= qingChiFan.getData().getTotal()) {
                        //禁用上拉加载更多功能
                        refreshLayout.setEnableLoadMore(false);
                    }
                    break;
                case TYPE_FAILURE:
                    Toast.makeText(HomeActivity.this, "" + msg.obj, Toast.LENGTH_SHORT).show();
                    //告诉刷新框架,已经刷新失败
                    refreshLayout.finishLoadMore(false);
                    refreshLayout.finishRefresh(false);
                    break;
            }

            return false;
        }
    });
}
